import java.util.*;
import java.util.concurrent.*;


// 单例模式： 懒汉形式
class SingletonLazy {
    private static volatile SingletonLazy instance =null;
    public SingletonLazy(){

    }
    public SingletonLazy getInstance() {
        if (instance == null) {
            synchronized (SingletonLazy.class) {
                if (instance == null) {
                    instance = new SingletonLazy();
                }
            }
        }
        return instance;
    }
}

// 阻塞队列
class MyBlockingQueue{
    private int[] queue=new int[4];
    private int size;
    private int head;
    private int tail;
    public synchronized void put(int a) throws InterruptedException {
        while(size == 4){
            this.wait();
        }
            queue[tail]=a;
            tail++;
            size++;
            tail%=4;
            this.notify();
    }
    public  synchronized  int take() throws InterruptedException {
        int ret=0;
        while(size == 0){
            this.wait();
        }
            ret =queue[head];
            head ++ ;
            head %=4;
            size--;
            this.notify();
        return ret;



    }
}


// 线程池
class ThreadPool{
    // 任务列表
    private BlockingQueue<Runnable> queue=new LinkedBlockingQueue<>();

    public void submit(Runnable a) throws InterruptedException {
        queue.put(a);
    }
    // 线程池中的线程数
    public ThreadPool(int n){
        for(int i=0;i<n;i++){
            Thread t=new Thread(()->{
                    try {
                        queue.take().run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

            });
            t.start();
        }
    }
}

// 定时器
class MyTimer{
    public Object locker =new Object();
    private PriorityQueue<MyTimerTask> queue=new PriorityQueue<>(10);

    public void schedule(Runnable r,long delay){
            synchronized (locker) {
                queue.offer(new MyTimerTask(r, delay));
                locker.notify();
            }
        };
    public MyTimer(){
        Thread t=new Thread(()->{
            while(true){
                synchronized (locker){
                    while(queue.isEmpty()){
                        try {
                            locker.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                    long time=System.currentTimeMillis();
                    MyTimerTask task=queue.poll();
                    if(task.getTime()<= time){
                        task.getRunnable().run();
                    }else{
                        try {
                            wait(time - task.getTime());
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        });
        t.start();
    }

}
class MyTimerTask implements  Comparable <MyTimerTask> {
    private long time;
    private Runnable runnable;

    public  MyTimerTask(Runnable r,long delay){
        this.runnable=r;
        this.time=delay+System.currentTimeMillis();
    }

    @Override
    public int compareTo(MyTimerTask o) {
        return (int)(this.time-o.time);
    }

    public long getTime() {
        return time;
    }

    public Runnable getRunnable() {
        return runnable;
    }
}
public class Test6 {

    public static void main(String[] args) {
        Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("a");
            }
        },100);

    }



    public static void main02(String[] args) throws InterruptedException {

        MyBlockingQueue myBlockingQueue=new MyBlockingQueue();
        Thread t1=new Thread(()->{
            try {
                myBlockingQueue.put(1);
                myBlockingQueue.put(2);
                myBlockingQueue.put(3);
                myBlockingQueue.put(4);
                myBlockingQueue.put(5);
                myBlockingQueue.put(6);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        });
        Thread t2=new Thread(()->{
            try {
                System.out.println(myBlockingQueue.take());
                System.out.println(myBlockingQueue.take());
                System.out.println(myBlockingQueue.take());
                System.out.println(myBlockingQueue.take());
                System.out.println(myBlockingQueue.take());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        });

        t1.start();
        t2.start();

    }




    static  int isQuite=0;

    public static void main01(String[] args) throws InterruptedException {
        Thread t1=new Thread(()->{
            while(isQuite == 0 ){
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        Thread t2=new Thread(()->{
            Scanner in=new Scanner(System.in);
            isQuite=in.nextInt();
        });
        t1.start();
        t2.start();
    }
}
